<?php
/**
 * Created by PhpStorm.
 * User: mawenhuan
 * Date: 17/6/11
 * Time: 上午11:03
 */

namespace App\Utils;


use App\Exceptions\HttpRequestException;

class HttpUtil
{

	public static function postJson($url, $content, $needLog=true, $tk)
	{
		$options = [
			CURLOPT_RETURNTRANSFER => true,
			CURLOPT_HEADER  => false,
			CURLOPT_CONNECTTIMEOUT => 50, // timeout on connect
			CURLOPT_TIMEOUT => 100, // timeout on response
			CURLOPT_POST => true,
			CURLOPT_POSTFIELDS => json_encode($content),
//			CURLOPT_CUSTOMREQUEST => 'POST',
			CURLOPT_HTTPHEADER => [
				"TK: {$tk}",
				"Content-Type: application/json"
			]
		];

		$ch = curl_init($url);
		if ($ch) {
			curl_setopt_array($ch, $options);
			$data = curl_exec($ch);
			$err = curl_error($ch);
			curl_close($ch);
			if ($err){
				throw new HttpRequestException($err);
			}
			if($needLog){
				$logInfo = [
					'url' => $url,
					'options' => $options,
					'params' => $content,
					'result' => $data,
					'error' => $err
				];
				\Log::info($logInfo);
//				var_dump($logInfo, $data, $err);
			}
			return $data;
		} else {
			throw new HttpRequestException("curl $url failed");
		}
	}

	public static function post($url, $content, $needLog = true, $options = [])
	{
		$options = [
			CURLOPT_RETURNTRANSFER => true,
			CURLOPT_HEADER  => false,
			CURLOPT_CONNECTTIMEOUT => 50, // timeout on connect
			CURLOPT_TIMEOUT => 100, // timeout on response
			CURLOPT_POST => true,
			CURLOPT_POSTFIELDS => json_encode($content),
			CURLOPT_CUSTOMREQUEST => 'POST',
		];

		$ch = curl_init($url);
		if ($ch) {
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
			curl_setopt_array($ch, $options);
			$data = curl_exec($ch);
			$err = curl_errno($ch);
			if ($err){
				throw new HttpRequestException($err);
			}
			curl_close($ch);
			if($needLog){
				$logInfo = [
					'url' => $url,
					'params' => $content,
					'result' => $data
				];
				\Log::debug($logInfo);
			}
			return $data;
		} else {
			throw new HttpRequestException("curl $url failed");
		}
	}


	public static function get($url, $content, $needLog=true)
	{
		$options = [
			CURLOPT_RETURNTRANSFER => true,
			CURLOPT_HEADER  => false,
			CURLOPT_CONNECTTIMEOUT => 50, // timeout on connect
			CURLOPT_TIMEOUT => 100, // timeout on response
			CURLOPT_POST => false,
			CURLOPT_CUSTOMREQUEST => 'GET',
		];

		$url .= '?' . http_build_query($content);

		$ch = curl_init($url);
		if ($ch) {
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt_array($ch, $options);
            $data = curl_exec($ch);
			curl_close($ch);
			if($needLog){
				$logInfo = [
					'url' => $url,
					'params' => $content,
					'result' => $data
				];
				\Log::debug(json_encode($logInfo));
			}
			return $data;
		} else {
			throw new HttpRequestException("curl $url failed");
		}
	}

	public static function httpRequest($url, $postData)
    {
        $postJsonData = json_encode($postData);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true);
        //curl_setopt($ch, CURLOPT_POST, 1);//post方式
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //TODO 线上去掉 HTTPS 信誉证书过滤
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postJsonData);
        curl_setopt($ch,CURLOPT_HTTPHEADER,[
            'Content-Type: application/json',
            'Content-Length: ' . strlen($postJsonData)
        ]);
        $response = curl_exec($ch);
        if($response === false) {
            echo '网络不稳定,请重试';
        }
        return $response;
    }
}
